package com.lrkj.back.service.impl;

import com.lrkj.back.entity.TbSeq;
import com.lrkj.back.mapper.TbSeqMapper;
import com.lrkj.back.service.TbSeqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.cbons.mamisay.parent.db.api.DBInterface;
import cn.com.cbons.mamisay.parent.exception.DBException;
import cn.com.cbons.mamisay.parent.service.base.ParentServiceImpl;

@Transactional
@Service
public class TbSeqServiceImpl extends ParentServiceImpl<TbSeq> implements TbSeqService {

    @Autowired
    private TbSeqMapper seqMapper;

    @Override
    protected DBInterface<TbSeq> getDao() throws DBException {
        return seqMapper;
    }

    @Override
    public TbSeq selectByType(String seqType) throws DBException {
        return seqMapper.selectByType(seqType);
    }

    @Override
    public Long seqVal(String seqType) throws DBException {
        TbSeq seq = seqMapper.selectByType(seqType);
        Long val = Long.valueOf(seq.getSeqValue());
        if(val == 9999||val<1000){
            val =1000l;
        }else{
            val ++;
        }
        seq.setSeqValue(val + "");
        seqMapper.updateByPrimaryKey(seq);
        return val;
    }

    @Override
    public Long[] seqValBatch(String seqType, int count) throws DBException {
        Long[] seqArrIntegers = new Long[count];
        TbSeq seq = seqMapper.selectByType(seqType);
        Long val = Long.valueOf(seq.getSeqValue());
        for (int i = 0; i < count; i++) {
            if(val == 9999||val<1000){
                val =1000l;
            }else{
                val++;
            }
            seqArrIntegers[i] = val;
        }
        seq.setSeqValue(val + "");
        seqMapper.updateByPrimaryKey(seq);
        return seqArrIntegers;
    }

}
